#!/bin/bash
#	xfilename=${a##*/} # result is with longest matching pattern of */ removed
# 	xfext=${xfilename#*.} # result is with longest matching pattern *. removeda from start
# 	xfpre=${xfilename%%.*} # result is with shortest matching patterin .* removed from end

# First things first, let's stop on errors, and stop on unset variables

set -o errexit
set -o nounset

# Want everything to go to log file... how?  Wrap it in (). Exec doesn't seem to work well

DATE=`date +"%Y%m%d-%H%M"`

log_filename="./${0##*/}.log-${DATE}"

(

echo "Log filename is ${log_filename}"

echo "Will install kazoo..."
echo " "
echo "NOTE:"
echo "   Suggest CentOS x64!"
echo " "
echo " "

echo "Please see: https://2600hz.atlassian.net/wiki/display/Dedicated/Manually"
echo "Please see: https://2600hz.atlassian.net/wiki/display/Dedicated/via+RPM"
echo " "
echo " "

# ----------------------------------------------------------------
# ----------------------------------------------------------------
#  These are all the functions.... 
# ----------------------------------------------------------------
# ----------------------------------------------------------------


# all the functions would go here...	

echo "including functions"
for sourcename in *.include 
do
#	echo "including file ... ${sourcename}"
	source ${sourcename}
done

# ----------------------------------------------------------------
# ----------------------------------------------------------------
# ----------------------------------------------------------------

function install_single_server() {
# Installs Kazoo on a single server...

echo "install Kazoo on a single server"

echo "See:  http://www.powerpbx.org/content/kazoo-v3-single-or-multiple-server-voip-telephony-platform-install-guide-v1"
echo ""
echo "1 Bigcouch Database server"
echo "1 Freeswitch Server"
echo "1 Kazoo Server"

echo " "

echo "Note: The most basic multiple server system for production consists of 7 servers:

3 Bigcouch Database Servers, configured as a cluster
2 Freeswitch Servers
2 Kazoo Servers
"

echo "
Consider the folowing separation of software between servers:
Bigcouch Database Servers: Bigcouch
Freeswitch Servers: HAProxy, Freeswitch
Kazoo Servers: HAProxy, RabbitMQ, Kamailio SIP Proxy, Kazoo eCall Manager, Kazoo Applications, Kazoo GUI
"


STEPS="
install_ss_prerequisites
install_ss_bigcouch
install_ss_haproxy
install_ss_freeswitch
install_ss_kazoo
install_ss_postinstall
install_ss_createaccount
"

STEPS="
install_config
install_ss_hardware
install_ss_prerequisites
install_ss_bigcouch
install_ss_freeswitch
install_ss_kazoo
install_ss_postinstall
install_ss_createaccount
"

do_installation_loop ${STEPS}

}

# ----------------------------------------------------------------
# ----------------------------------------------------------------
# ----------------------------------------------------------------

function install_via_rpm_method() {
# Install Kazoo via rpm method...
# Per https://2600hz.atlassian.net/wiki/display/Dedicated/via+RPM
#
# Per this guide, there are three main sections:
#   Deploying a Kazoo v3.0 cluster
#      1) Prerequisites for every server
#      2) Bigcouch Servers
#      3) FreeSWITCH Server
#      4) Kazoo Server
# then
#      5) Post Installation
#


	install_rpm_prerequisites
	install_rpm_bigcouch_servers
	install_rpm_freeswitch_server
	install_rpm_kazoo_server
	install_rpm_post_installation

exit

} # end of install_via_rpm_method

# ----------------------------------------------------------------
# ----------------------------------------------------------------
# ----------------------------------------------------------------



function install_via_alt_method() {
	echo "RPM method from atlassian doesn't work... "
	echo " No package kazoo-bigcouch-R15B available."

	echo "" 
echo_separator "Installing ERLANG"
install_erlang

echo_separator "Installing AMQP"
install_amqp

echo_separator "Installing bigcouch servers"
install_bigcouch

echo_separator "Installing haproxy"
install_haproxy

echo_separator "Installing Freeswitch"
install_freeswitch 

echo_separator "Installing Kazoo"

} # end function install_via_rmp_method_alt() {

# ----------------------------------------------------------------
# ----------------------------------------------------------------
# ----------------------------------------------------------------

function install_via_manual_method() {

STEPS="
install_manual_erlang
install_manual_amqp
install_manual_bigcouch
install_manual_freeswitch
install_manual_kazoo
install_manual_kazoo_ui
install_manual_kamailio
install_manual_opensips
"

do_installation_loop ${STEPS}

} # end of install_via_manual_method

# ----------------------------------------------------------------
# ----------------------------------------------------------------
# ----------------------------------------------------------------

opt="${1:-}"

echo " Option selected : ${opt}"

case "${opt}" in
s*)
	echo "Install Single Server"
	install_single_server
;;


r*)
	echo "option RPM selected!"
echo "doesn't work"
exit 1

	install_via_rpm_method

;;

m*)
	echo "option MANUAL selected!"

	install_via_manual_method


;;

a*) 
        echo "option ALTERNATE selected!"
echo "doesn't work"
exit 1

        install_via_alt_method

;;
	

1*) 
	echo "This is only for testing purposes and will install just one package..."

#	echo "install_bigcouch"
#	install_bigcouch

 echo_separator "Installing Freeswitch"
install_freeswitch "${SYSTEM_32_OR_64}"

;;

	
*)

	echo " "
	echo "Please verify selinux is disabled with sestatus"
	echo "Please ensure that each node has a FQDN with hostname -f"
	echo " "
	echo "options:"
	echo " m :  To run manuual version of installation, use option m"
	echo " r :  To run rpm version of installation, use option r "
	echo " s :  To install on a single server"
	echo " "
	echo "${0} m | r"
;;

esac


exit 0


) 2>&1 | tee "./${log_filename}"


exit 0


